﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GatewayInspect.Common;
using GatewayInspect.Model;

namespace GatewayInspect.Modules
{
    public partial class SelectTruckForm : PopupForm
    {
        private DataTable _dataSourceTable;

        public DataRow SelectedRow { get; set; }

        public SelectTruckForm()
        {
            InitializeComponent();
        }

        public SelectTruckForm(DataTable _table)
            : this()
        {
            _dataSourceTable = _table;
        }

        private void SelectTruckForm_Load(object sender, EventArgs e)
        {
            if (_dataSourceTable != null)
            {
                this.cdgvTruck.AutoGenerateColumns = false;
                this.cdgvTruck.DataSource = _dataSourceTable;
            }
        }

        private void cdgvTruck_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (cdgvTruck.SelectedCells != null && cdgvTruck.SelectedCells.Count > 0 && _dataSourceTable != null)
            {
                string strID = cdgvTruck.SelectedCells[0].OwningRow.Cells["ID"].Value.ToString();
                DataRow[] rows = _dataSourceTable.Select("ID = '" + strID + "'");
                if (rows != null && rows.Length > 0)
                {
                    SelectedRow = rows[0];
                    this.DialogResult = DialogResult.OK;
                }
            }
        }

        private void btnLink_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtTruckNo.Text) && _dataSourceTable !=null && _dataSourceTable.Rows.Count > 0)
            {
                DataTable dtTruck = DataAccess.ExecuteDataTable("select * from T_TRUCK_INFO where ORGCODE = '" + Session.CurrentOrgCode + "' and TRUCKNO = '" + txtTruckNo.Text + "'");
                if (dtTruck != null && dtTruck.Rows.Count > 0)
                {
                    DataTable dt = LinkTruck(_dataSourceTable.Rows[0], dtTruck.Rows[0]);
                    if (dt != null)
                    {
                        MessageBox.Show("关联成功！");
                        _dataSourceTable.Merge(dt);
                        this.cdgvTruck.AutoGenerateColumns = false;
                        this.cdgvTruck.DataSource = _dataSourceTable;
                    }
                }
                else
                {
                    MessageBox.Show("未找到对应车辆信息！");
                }
            }
        }

        private DataTable LinkTruck(DataRow rowDriver, DataRow rowTruck)
        {
            TruckInfo info = TruckInfo.GetInstanceFromDB(rowTruck);
            info.ID = string.Empty;//按新增处理
            info.DRIVERID = rowDriver["DRIVERID"].ToString();
            info.DRIVERNAME = rowDriver["DRIVERNAME"].ToString();
            info.JSZ_NAME = rowDriver["JSZ_NAME"].ToString();
            info.JSZ_NO = rowDriver["JSZ_NO"].ToString();
            info.CYZGZ_JSYZ.ID = rowDriver["CYZGZ_JSYZ_ID"].ToString();
            DateTime dtTemp;
            if (DateTime.TryParse(rowDriver["JSZ_YXQ"].ToString(), out dtTemp))
                info.JSZ_YXQ = dtTemp;
            if (DateTime.TryParse(rowDriver["CYZGZ_JSYZ_JYJL"].ToString(), out dtTemp))
                info.CYZGZ_JSYZ.JXJYJL = dtTemp;
            if (DateTime.TryParse(rowDriver["CYZGZ_JSYZ_CXKH"].ToString(), out dtTemp))
                info.CYZGZ_JSYZ.CXKH = dtTemp;
            if (DateTime.TryParse(rowDriver["CYZGZ_JSYZ_YXQ"].ToString(), out dtTemp))
                info.CYZGZ_JSYZ.YXQ = dtTemp;
            string sID = string.Empty;
            if (TruckInfo.SaveToDB(info, ref sID))
            {
                return DataAccess.ExecuteDataTable("select * from T_TRUCK_INFO where ID = '" + sID + "'");
            }
            return null;
        }
    }
}
